<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Askleave extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'askleave';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'ask_time_text',
        'check_text'
    ];
    

    
    public function getCheckList()
    {
        return ['0' => __('Check 0'), '1' => __('Check 1'), '2' => __('Check 2')];
    }


    public function getAskTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ask_time']) ? $data['ask_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCheckTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check']) ? $data['check'] : '');
        $list = $this->getCheckList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setAskTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
